package com.zq.it.action.admin;

import java.util.List;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.pojo.Teacher;
import com.zq.it.pojo.User;
import com.zq.it.service.admin.TeacherService;
import com.zq.it.tool.Constant;
import com.zq.it.tool.FileName;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

public class TeacherAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	@Resource
	private TeacherService teacherService;

	private Teacher teacher;
	// private String type;
	private String parameter;
	private Integer teacherId;
	private long[] teacherGroupId;

	public void searchTeacherList() {
		List list = teacherService.getAllTeacher();
		uploadList(com.zq.it.tool.EtyLsName.TEARCHER_LS.name(), list);
		print(toJSON(list).toString(), "UTF-8", "APPLICATION/JSON");
	}
	public void modifyTeacher() {
		teacherService.renewalTeacher(teacher);
		print("<script>parent.closeWindow('已修改成功！');</script>", "UTF-8", "text/html");
	}
	
	public String modifyTeacherInfo() {
		teacherService.renewalTeacher(teacher);
		return "teacherInfo";
	}
	
	public void addTeacherPo() {
		teacherService.addTeacher(teacher);
		print("<script>parent.closeWindow('已添加');</script>", "UTF-8",
				"text/html");
	}
	public void deleteTeacherGroups() {

		teacherService.removeAllTeacher(teacherGroupId);
		print("已删除" + teacherGroupId.length + "条记录！", "UTF-8", "text/html");
	}
	public void deleteTeacher() {
		teacherService.removeTeacher(teacherId);
		print("已删除一条记录！", "UTF-8", "text/html");
	}

	public void searchTeacherListFromType() {
		List list = teacherService.getTeacherListByType(parameter);
		uploadList(com.zq.it.tool.EtyLsName.TEARCHER_LS.name(), list);
		print(toJSON(list).toString(), "UTF-8", "APPLICATION/JSON");
	}
	
	public void validateLogin() {
		if (teacher == null) {
			teacher = new Teacher();
			teacher.setTeacherId(Integer.parseInt(getAccount()));
			teacher.setTeacherPassword(getPassword());
		}
		List list = teacherService.getTeacherByExmp(teacher);
		if (list.size() > 0) {
			teacher = (Teacher)list.get(0);
		} else {
			addFieldError("account", "用户不存在或密码不正确！");
		}
}

	public String getTeacherInfo() {
		return "teacherInfo";
	}
	public String login() {
			getHttpSession().setAttribute(Constant.TEACHER, teacher);
			getHttpSession().setAttribute(Constant.USER, new User(teacher.getTeacherId().toString()));
			return SUCCESS;
	}

	public String exit() {
		getHttpSession().removeAttribute(Constant.USER);
		getHttpSession().removeAttribute(Constant.TEACHER);
		return INPUT;
	}

	public void importTeacherList() {
		String resultStr = "";
		List<Teacher> list = teacherService.importTeacherList(getUpload());

		if (list.size() > 0) {
			getRequest().getSession().setAttribute("texistList", list);
			resultStr = "existList";
		} else {
			resultStr = "已导入成功!";
		}
		print("<script>parent.importBackResult('" + resultStr
				+ "','webpage/adminpage/teacher/texistList.jsp');</script>",
				"UTF-8", "TEXT/HTML");
	}

	public void exportTeacherList() {
		download(
				teacherService.exportTeacherList(
						HeadTitle.CN_TEACHERHEAD.getArray(),
						(List) getHttpSession().getAttribute(
								com.zq.it.tool.EtyLsName.TEARCHER_LS.name())),
				FileName.TEACHER_LIST_NAME.toString() + FileType.XLSX);
	}

	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public long[] getTeacherGroupId() {
		return teacherGroupId;
	}
	public void setTeacherGroupId(long[] teacherGroupId) {
		this.teacherGroupId = teacherGroupId;
	}
	public String getParameter() {
		return parameter;
	}
	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	public Integer getTeacherId() {
		return teacherId;
	}
	public void setTeacherId(Integer teacherId) {
		this.teacherId = teacherId;
	}

}
